package cn.imnaer.service.rpc.scenic.impl;

import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.persistence.model.ScenicContribution;
import cn.imnaer.service.persistence.model.consts.SCENIC;
import cn.imnaer.service.rest.view.model.PageInfo;
import cn.imnaer.service.rpc.scenic.ScenicComponentRpcApi;
import cn.imnaer.service.rpc.scenic.dao.ScenicContributionDaoImpl;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;
import cn.imnaer.service.rpc.scenic.process.ScenicSearch;

public class ScenicComponentRpcApiImpl implements ScenicComponentRpcApi {
	private ScenicDaoImpl scenicDao = ScenicDaoImpl.getInstance();
	private ScenicContributionDaoImpl scenicContributionDao = ScenicContributionDaoImpl.getInstance();
	@Override
	public PageInfo<Scenic> getScenicListByCityCode(String cityCode, int currentPage) {
		PageInfo<Scenic> pageInfo = new PageInfo<Scenic>();
		BSONObject matcher = new BasicBSONObject();
		matcher.put(SCENIC.cityCode, cityCode);
		int pageCount = this.scenicDao.getPageCount(matcher);
		List<Scenic> data = this.scenicDao.findPerPage(matcher, null, currentPage);
		pageInfo.setPageCount(pageCount);
		pageInfo.setData(data);
		return pageInfo;
	}

	@Override
	public ScenicContribution publishContribution(ScenicContribution contribution) {
		return this.scenicContributionDao.insert(contribution, true);

	}

	@Override
	public Scenic getScenicById(String scenicId) {
		return this.scenicDao.findOneByPk(scenicId);
	}

	@Override
	public List<ScenicContribution> getScenicContributionList(String scenicId) {
		BSONObject matcher = new BasicBSONObject();
		matcher.put("scenicId", scenicId);
		List<ScenicContribution> list = this.scenicContributionDao.findAll(matcher, null);
		return list;
	}

	@Override
	public PageInfo<Scenic> search(String keyword,int page) {
   BSONObject query = new ScenicSearch().getQuery(keyword);
   PageInfo<Scenic> pageInfo = new PageInfo<Scenic>();
   List<Scenic> data = this.scenicDao.findPerPage(query, null, page);
	int pageCount = this.scenicDao.getPageCount(query);
	pageInfo.setData(data);
	pageInfo.setPageCount(pageCount);
		return pageInfo;
	}

	@Override
	public List<ScenicContribution> getUserContributionsByUserId(String userId) {
		return this.scenicContributionDao.getUserContributionsByUserId(userId);
	}

	@Override
	public List<Scenic> getMyContributeScenics(String userId) {
		
		List<String> scenicIds = this.scenicContributionDao.getMyContributScenicsByUserId(userId);
		return this.scenicDao.findListByPkArray(scenicIds);
	}

	@Override
	public List<Scenic> getScenicListByScenicGroupId(String scenicGroupId, int currentPage) {
		return null;
	}

	
}
